//
//  WYNetWorking.m
//  WYModel
//
//  Created by ios on 2017/3/3.
//  Copyright © 2017年 Wy. All rights reserved.
//

#import "WYNetWorking.h"

@implementation WYNetWorking

+(instancetype)shareInstance{
    static WYNetWorking *manger = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manger = [[WYNetWorking alloc] init];
    });
    return manger;
}

-(void)Get:(NSString *)url pargmeter:(NSDictionary *)parameters success:(void (^)(id ))success failure:(void (^)(NSError *))failure{
    
    AFHTTPSessionManager *manger = [AFHTTPSessionManager manager];
    manger.responseSerializer    = [AFHTTPResponseSerializer serializer];
    
    [manger GET:url parameters:parameters progress:^(NSProgress * _Nonnull downloadProgress){

     //   [SVProgressHUD show];
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
     //   [SVProgressHUD dismiss];
        NSString *result         = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        
        if (success) {
            success([self parseJSONStringToNSDictionary:result]);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
         failure(error);
        
         [SVProgressHUD showErrorWithStatus:@"网络异常"];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            [SVProgressHUD dismiss];
            
        });
    }];
    
}

-(void)Post:(NSString *)url pargmeter:(NSDictionary *)parameters success:(void (^)(id))success failure:(void (^)(NSError *))failure{
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer    = [AFHTTPResponseSerializer serializer];
    NSMutableSet *contentType    = [[NSMutableSet alloc] initWithSet:manager.responseSerializer.acceptableContentTypes];
    
    [contentType addObject:@"text/html"];
    [contentType addObject:@"application/json"];
    [contentType addObject:@"charset=utf-8"];
    
    manager.responseSerializer.acceptableContentTypes = contentType;
    
    [manager POST:url parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
        
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        success(dic);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
       
        failure(error);
        
        [SVProgressHUD showErrorWithStatus:@"网络异常"];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            [SVProgressHUD dismiss];
        });
        
    }];
    
}


-(void)PostFile:(NSString *)url parameter:(NSDictionary *)parameter data:(NSData *)fileData name:(NSString *)name fileName:(NSString *)fileName mimeType:(NSString *)mimeType success:(void (^)(id))success failure:(void (^)(NSError *))failure{
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer    = [AFHTTPResponseSerializer serializer];
    NSMutableSet *contentTypes    = [[NSMutableSet alloc] initWithSet:manager.responseSerializer.acceptableContentTypes];
    [contentTypes addObject:@"text/html"];//添加类型
    [contentTypes addObject:@"application/json"];
    [contentTypes addObject:@"charset=utf-8"];
    manager.responseSerializer.acceptableContentTypes = contentTypes;
    
    [manager POST:url parameters:parameter constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
       
        [formData appendPartWithFileData:fileData name:name fileName:fileName mimeType:mimeType]; //image/jepg
        
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        //上传成功
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        //上传失败
        
        failure(error);
        
        [SVProgressHUD showErrorWithStatus:@"上传失败"];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            [SVProgressHUD dismiss];
        });
        
    }];
    
    
    
    
}


-(NSDictionary *)parseJSONStringToNSDictionary:(NSString *)JSONString {
    
    NSData *JSONData = [JSONString dataUsingEncoding:NSUTF8StringEncoding];
    NSDictionary *responseJSON = [NSJSONSerialization JSONObjectWithData:JSONData options:NSJSONReadingMutableLeaves error:nil];
    
    return responseJSON;
}


@end
